package com.zhxi.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhxi.common.utils.PageUtils;
import com.zhxi.common.utils.Query;
import com.zhxi.common.utils.R;
import com.zhxi.gulimall.product.dao.BrandDao;
import com.zhxi.gulimall.product.entity.BrandEntity;
import com.zhxi.gulimall.product.service.BrandService;
import com.zhxi.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 品牌
 *
 * @Author: zhxi
 * @Email: 664480310@qq.com
 * @Date: 2024-08-19 17:41:15
 * @Version: 1.0.0
 */
@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService relationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public R updateByDetail(BrandEntity brand) {
        updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())) {
            relationService.updateBrand(brand.getBrandId(), brand.getName());
        }
        return R.ok();
    }

}